package cn.sskxyz.mongodb.compress;

import com.mongodb.MongoNamespace;

public class DefaultCompressKeyMatcher implements CompressKeyMatcher {

	@Override
	public boolean match(CompressKey compressKey, MongoNamespace namespace) {
		boolean currentDynamic = checkCurrentDynamic(namespace.getDatabaseName());

		if (currentDynamic && compressKey.isDynamic()) {
			return namespace.getCollectionName().equals(compressKey.getCollection());
		}

		if (currentDynamic == compressKey.isDynamic()) {
			boolean tableEqual = namespace.getCollectionName().equals(compressKey.getCollection());
			return tableEqual && namespace.getDatabaseName().equals(compressKey.getDatabase());
		}
		return false;
	}

	public boolean checkCurrentDynamic(String databaseName) {
		if (databaseName.startsWith("dynamic_")) {
			return true;
		}
		return false;
	}
}
